package org.gd136.ce.script.impl;

import org.gd136.ce.script.Script;
import org.gd136.ce.script.ScriptStage;
import org.gd136.ce.util.StringUtil;

import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.ParallelAction;
import com.badlogic.gdx.utils.Align;

public class EnterStageScript extends Script {

	String actorName;
	ParallelAction actions = Actions.parallel();
	float x, y;
	int align = Align.bottomLeft;
	boolean isFadeIn = false;

	@Override
	public Script readScript(String scriptLine) {
		EnterStageScript ess = null;
		if (scriptLine.length() > 1 && scriptLine.startsWith(">")) {
			ess = new EnterStageScript();
			int p = scriptLine.indexOf(' ');
			if (p < 0) {
				ess.actorName = scriptLine.substring(1).trim();
			} else {
				ess.actorName = scriptLine.substring(1, p);
				Argument arg = null;
				while ((arg = readNextArguments(scriptLine, p)) != null) {
					ess.addAction(arg);
					p = arg.endIndex;
				}
			}
			ess.addToPoolAndLinkFrontScript();
		}
		return ess;
	}

	private void addAction(Argument arg) {
		if (arg == null) {
			return;
		}
		switch (arg.argumentName) {
		case "fade": {
			float delay = 0.4f;
			if (arg.arguments != null) {
				StringUtil.parseFloat(arg.arguments[0], 0.4f);
			}
			actions.addAction(Actions.fadeIn(delay));
			isFadeIn = true;
		}
			break;
		case "position": {
			if (arg.arguments != null) {
				x = StringUtil.parseFloat(arg.arguments[0], 0);
				if (arg.arguments.length > 1) {
					y = StringUtil.parseFloat(arg.arguments[1], 0);
				}
				if (arg.arguments.length > 2) {
					switch (arg.arguments[2]) {
					case "left":
						align = Align.left;
						break;
					case "center":
						align = Align.center;
						break;
					case "right":
						align = Align.right;
						break;
					case "bottomLeft":
						align = Align.bottomLeft;
						break;
					case "bottom":
						align = Align.bottom;
						break;
					case "bottomRight":
						align = Align.bottomRight;
						break;
					case "topLeft":
						align = Align.topLeft;
						break;
					case "top":
						align = Align.top;
						break;
					case "topRight":
						align = Align.topRight;
						break;
					}
				}
			}
		}
			break;
		case "moveBy": //TODO: ..
		case "moveTo":
		case "rotateBy":
		case "rotateTo":
		}
	}

	@Override
	public void execute(ScriptStage stage) {
		stage.enterStage(actorName, actions, x, y, align, isFadeIn);
	}

}
